package cn.sdormitory.report.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.constant.CommonConstant;
import cn.sdormitory.report.dao.AttenceStatisticsDao;
import cn.sdormitory.report.service.AttenceStatisticsService;
import cn.sdormitory.report.vo.AttenceStatisticsVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @创建人：zhouyang
 * @创建时间：2020/12/3 19:11
 * @version：V1.0
 */
@Slf4j
@Service("attenceStatisticsService")
public class AttenceStatisticsServiceImpl extends ServiceImpl<AttenceStatisticsDao, AttenceStatisticsVO> implements AttenceStatisticsService {
    @Resource
    AttenceStatisticsDao attenceStatisticsDao;

    @Override
    public CommonPage<AttenceStatisticsVO> getAttenceStaList(Map<String,Object> params,int pageNum,int pageSize) {
        List<String> dateList = new ArrayList<>();
        if(StringUtils.isEmpty(params.get("accessDate"))){
//        if(StringUtils.isEmpty(params.get("beginDate")) || StringUtils.isEmpty(params.get("endDate"))){
            Date date = new Date();
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            String date1=fmt.format(date);
            dateList.add(date1);
        }else{
//           dateList = getDiffDateList((String)params.get("beginDate"),(String)params.get("endDate"));
            dateList.add((String)params.get("accessDate"));
        }
        int currIndex=(pageNum-1)*pageSize;
        List<AttenceStatisticsVO>  taskTotalVoList = attenceStatisticsDao.getAttenceStaList(dateList,currIndex,pageSize);
        CommonPage<AttenceStatisticsVO> commonPage = new CommonPage<AttenceStatisticsVO>();
        commonPage.setList(taskTotalVoList);
        Long count = this.baseMapper.selectTotalCountByDate(dateList);
        commonPage.setTotal(count);

        return commonPage;
    }

    private List<String> getDiffDateList(String beginDate,String endDate){
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dateList = new ArrayList<>();
        try {
            Date star = dft.parse(beginDate);//开始时间
            Date endDay=dft.parse(endDate);//结束时间

            Calendar beginCalendar = new GregorianCalendar();
            beginCalendar.setTime(star);

            Calendar endCalendar = new GregorianCalendar();
            endCalendar.setTime(endDay);
            dateList.add(beginDate);
            while(beginCalendar.compareTo(endCalendar)<0){
                beginCalendar.add(Calendar.DATE,1);
                Date date = beginCalendar.getTime();
                dateList.add(dft.format(date));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateList;
    }

    @Override
    public List<AttenceStatisticsVO> getAttenceStaListExport(Map<String,Object> params) {
        List<String> dateList = new ArrayList<>();
        if(StringUtils.isEmpty(params.get("accessDate"))){

//        if(StringUtils.isEmpty(params.get("beginDate")) || StringUtils.isEmpty(params.get("endDate"))){
            Date date = new Date();
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            String date1=fmt.format(date);
            dateList.add(date1);
        }else{
//            dateList = getDiffDateList((String)params.get("beginDate"),(String)params.get("endDate"));
            dateList.add((String)params.get("accessDate"));
        }
        return attenceStatisticsDao.getAttenceStaListExport(dateList);
    }

}
